package com.mazaiting

import java.awt.*
import java.awt.event.*
import java.lang.Exception
import java.lang.StringBuilder
import javax.swing.*
import javax.swing.JTextField
import javax.swing.JFrame
import kotlin.collections.ArrayList
import kotlin.random.Random


/**
 * 程序入口
 */
object AppMain : ActionListener {
    /**
     * 数目输入
     */
    private lateinit var numTextField: JTextField
    /**
     * 最小值输入
     */
    private lateinit var minTextField: JTextField
    /**
     * 最大值输入
     */
    private lateinit var maxTextField: JTextField

    @JvmStatic
    fun main(args: Array<String>) {
        println("随机数生成器")

        frame()
    }

    /**
     * 主窗口
     */
    private fun frame() {
        val frame = JFrame("随机数生成器")
        // 消息提示
        val msgLabel = JLabel("随机数的数目，最小值和最大值：")
        // 数目
        val numLabel = JLabel("数目：")
        numTextField = JTextField(10)
        // 最小值
        val minLabel = JLabel("最小值：")
        minTextField = JTextField(10)
        // 最大值
        val maxLabel = JLabel("最大值：")
        maxTextField = JTextField(10)
        val button = JButton("生成")
//        button.addActionListener(ProductListener())
        button.addMouseListener(MouseClickListener())
        // 快捷键注册
        button.registerKeyboardAction(
            this,
            KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK),
            JComponent.WHEN_IN_FOCUSED_WINDOW
        )
        button.addActionListener(this)
        //使用网格布局
        frame.layout = GridLayout(6, 2)
        frame.add(msgLabel)
        frame.add(JLabel(""))
        frame.add(numLabel)
        frame.add(numTextField)
        frame.add(minLabel)
        frame.add(minTextField)
        frame.add(maxLabel)
        frame.add(maxTextField)
        frame.add(button)
        frame.setSize(400, 200)
        frame.setLocation(450, 350)
        frame.isVisible = true
    }

    /**
     * 显示对话框
     */
    private fun showDialog(title: String = "提示", msg: String) {
        JOptionPane.showMessageDialog(
            null,
            msg, title, JOptionPane.INFORMATION_MESSAGE
        )
    }

    /**
     * 生成随机数
     * @param num 数目
     * @param min 最小值
     * @param max 最大值
     */
    private fun productRandom(num: Int, min: Int, max: Int) {
        if (num > 0) {
            if (min < max) {
                val list = ArrayList<Int>()
                for (i in 0 until num) {
                    while (true) {
                        val random = Random.nextInt(min, max)
                        var flag = true
                        if (list.size > 0) {
                            list.forEach { index ->
                                if (index == random) {
                                    flag = false
                                }
                            }
                        }
                        if (flag) {
                            list.add(random)
                            break
                        }
                    }
                }
                val builder = StringBuilder()
                list.forEach { index -> builder.append(index).append(" ") }
                showDialog(title = "生成的随机数如下", msg = builder.toString())
            } else {
                showDialog(msg = "最大值不能小于或等于最小值")
            }
        } else {
            showDialog(msg = "生成的随机数目不能小于0")
        }
    }

    /**
     * 获取数字
     */
    private fun getNum(text: String?): Int {
        return try {
            if (null == text || text == "") {
                Integer.MIN_VALUE
            } else {
                text.toInt()
            }
        } catch (e: Exception) {
            Integer.MIN_VALUE
        }
    }

    override fun actionPerformed(e: ActionEvent?) {
        println(e?.source)
    }

    /**
     * 生成监听
     */
    class ProductListener : ActionListener {
        override fun actionPerformed(e: ActionEvent?) {
//            println(e.toString())
            onLeftClick()
        }
    }

    /**
     * 左键点击
     */
    private fun onLeftClick() {
        val num = getNum(numTextField.text)
        val min = getNum(minTextField.text)
        val max = getNum(maxTextField.text)

        if (num == Integer.MIN_VALUE) {
            showDialog(msg = "请输入正确的数目!")
            return
        }

        if (min == Integer.MIN_VALUE) {
            showDialog(msg = "请输入正确的最小值")
            return
        }

        if (max == Integer.MIN_VALUE) {
            showDialog(msg = "请输入正确的最大值")
            return
        }

        println(num)
        println(min)
        println(max)

        productRandom(num, min, max)
    }

    /**
     * 右键点击
     */
    fun onRightClick() {
        showDialog(msg = "右键点击")
    }

    /**
     * 鼠标点击监听，区分左右键
     */
    class MouseClickListener : MouseAdapter() {
        override fun mouseClicked(e: MouseEvent?) {
            when (e?.button) {
                MouseEvent.BUTTON1 -> onLeftClick()
                MouseEvent.BUTTON3 -> onRightClick()
                else -> {
                }
            }
        }

    }
}

